package com.cyys.modules.system.controller.org;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.AjaxJson;
import com.cyys.common.ql.JSONData;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.system.model.user.User;
import com.cyys.modules.system.service.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/orgUser")
public class OrgUserAction extends BaseAction {

	@Resource
	private UserService userService;

	@RequestMapping("/list")
	@RequiresPermissions("sys:orgUser:list")
	public String list(Model model) {
//		model.addAttribute("isManage", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.YES_NO));
		return "module/system/org/orgUser-list";
	}

	@RequestMapping("/add/input")
	@RequiresPermissions("sys:orgUser:add")
	public String addInput(Model model,String orgId) throws Exception {
		User user = new User();
		if(StringUtils.isNotBlank(orgId)){
			user.setOrgId(orgId);
		}
		model.addAttribute("user", user);
//		model.addAttribute("isManage", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.YES_NO));
		return "module/system/org/orgUser-input";
	}
	

	@ResponseBody
	@RequestMapping("/add/save")
	@RequiresPermissions(value = {"sys:orgUser:add", "sys:orgUser:edit"},logical=Logical.OR)
	@SystemLog(module = "机构管理", methods = "机构用户管理-保存机构用户")
	public AR save(String userId, String orgId) throws Exception {
		User user = new User();
		user.setId(userId);
		user.setOrgId(orgId);
		return userService.update(user);
	}

	@ResponseBody
	@RequestMapping("/list/listInfo")
	@RequiresPermissions("sys:orgUser:list")
	@SystemLog(module = "机构管理", methods = "机构用户管理-查询机构用户")
	public String listInfo(User user, Page page) throws Exception {
		if(getUser().getType().equals("1")){
			return JSONData.toLayerData(userService.list(page, new QueryWrapper(user),true));
		}else{
			user.setOrgId(getUser().getOrgId());
			return JSONData.toLayerData(userService.list(page, new QueryWrapper(user),true));
		}
	}

	@ResponseBody
	@RequestMapping("/del/deleteById")
	@RequiresPermissions("sys:orgUser:del")
	@SystemLog(module = "机构管理", methods = "机构用户管理-删除机构用户")
	public AjaxJson deleteById(String userId) throws Exception{
		return null;

//		return orgUserService.deleteById(id);
	}

	@ResponseBody
	@RequestMapping("/del/deleteByIds")
	@RequiresPermissions("sys:orgUser:del")
	@SystemLog(module = "机构管理", methods = "机构用户管理-批量删除机构")
	public AjaxJson deleteByIds(String ids) throws Exception {
		return null;
//		return orgUserService.deleteBatchIds(ids);
	}
}
